package cn.edu.sdjzu.xg23.kcsj01_backend.service;

import cn.edu.sdjzu.xg23.kcsj01_backend.mapper.LiteratureDatabaseMapper;
import cn.edu.sdjzu.xg23.kcsj01_backend.utils.*;
import cn.edu.sdjzu.xg23.kcsj01_backend.entity.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.List;

@Service
public class LiteratureDatabaseService {
    @Autowired
    private LiteratureDatabaseMapper literatureDatabaseMapper;
    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    public PageInfo<LiteratureDatabase> get(int pageNum, int pageSize, LiteratureDatabase literatureDatabase) {
        PageHelper.startPage(pageNum, pageSize);
        List<LiteratureDatabase> literatureDatabaseList = literatureDatabaseMapper.get(literatureDatabase);
        return PageInfo.of(literatureDatabaseList);
    }

    @Transactional
    public String save(LiteratureDatabase literatureDatabase) {
        if( literatureDatabase.getId() == null){
            Long id = snowflakeIdGenerator.generateId();


            literatureDatabase.setId(id);
        }


        if (literatureDatabaseMapper.save(literatureDatabase)) {
            return "保存成功";
        }
        return "保存失败";
    }

    public String update(LiteratureDatabase literatureDatabase) {
        if (literatureDatabaseMapper.update(literatureDatabase)) {
            return "修改成功";
        }
        System.out.println("修改失败");
        return "修改失败";
    }

    public String delete(long id) {
        if (literatureDatabaseMapper.delete(id)) {
            return "删除成功";
        }
        return "删除失败";
    }
}
